﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jayden.Dll.CodeGen;

namespace Jayden.Windows.CodeGen.Controls
{
	public partial class MembersEditor : UserControl
	{
		public MembersEditor()
		{
			InitializeComponent();

			for (int c = 0; c < dgvMembers.Rows.Count; c++)
				ResetRow(dgvMembers.Rows[c]);

			dgvMembers.RowsAdded += new DataGridViewRowsAddedEventHandler(RowsAdded);
		}

		private void RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			DataGridViewRow row = dgvMembers.Rows[e.RowIndex];
			ResetRow(row);
		}

		private void ResetRow(DataGridViewRow row)
		{
			DataGridViewComboBoxCell accessCell = row.Cells[0] as DataGridViewComboBoxCell;
			DataGridViewTextBoxCell typeCell = row.Cells[1] as DataGridViewTextBoxCell;
			DataGridViewTextBoxCell nameCell = row.Cells[2] as DataGridViewTextBoxCell;
			DataGridViewTextBoxCell defaultCell = row.Cells[3] as DataGridViewTextBoxCell;
			DataGridViewCheckBoxCell getterCell = row.Cells[4] as DataGridViewCheckBoxCell;
			DataGridViewCheckBoxCell setterCell = row.Cells[5] as DataGridViewCheckBoxCell;
			DataGridViewLinkCell viewCell = row.Cells[6] as DataGridViewLinkCell;

			accessCell.Value = "private";
			nameCell.Value = "";
			defaultCell.Value = "";
			getterCell.Value = false;
			setterCell.Value = false;
			typeCell.Value = "object";
			viewCell.Value = "view";
		}

		private void SetRow(Member member, DataGridViewRow row)
		{
			DataGridViewComboBoxCell accessCell = row.Cells[0] as DataGridViewComboBoxCell;
			DataGridViewTextBoxCell typeCell = row.Cells[1] as DataGridViewTextBoxCell;
			DataGridViewTextBoxCell nameCell = row.Cells[2] as DataGridViewTextBoxCell;
			DataGridViewTextBoxCell defaultCell = row.Cells[3] as DataGridViewTextBoxCell;
			DataGridViewCheckBoxCell getterCell = row.Cells[4] as DataGridViewCheckBoxCell;
			DataGridViewCheckBoxCell setterCell = row.Cells[5] as DataGridViewCheckBoxCell;
			DataGridViewLinkCell viewCell = row.Cells[6] as DataGridViewLinkCell;

			accessCell.Value = member.Access;
			nameCell.Value = member.MemberName;
			defaultCell.Value = member.DefaultValue;
			getterCell.Value = member.Getter;
			setterCell.Value = member.Setter;
			typeCell.Value = member.Type;
			viewCell.Value = "view";
		}

		public void BuildFromMembers(List<Member> members)
		{
			dgvMembers.Rows.Clear();
			for (int c = 0; c < members.Count; c++)
			{
				dgvMembers.Rows.Add();
				SetRow(members[c], dgvMembers.Rows[c]);
			}
		}

		public List<Member> BuildMembers()
		{
			List<Member> members = new List<Member>();
			for (int c = 0; c < dgvMembers.Rows.Count; c++)
			{
				DataGridViewRow row = dgvMembers.Rows[c];
				if(!string.IsNullOrWhiteSpace(row.Cells[2].Value.ToString()))
				{
					string access = row.Cells[0].Value.ToString();
					string type = row.Cells[1].Value.ToString();
					string name = row.Cells[2].Value.ToString();
					Member member = new Member(name, type, access);
					member.DefaultValue = (row.Cells[3].Value ?? string.Empty) .ToString();
					member.Getter = (bool)row.Cells[4].Value;
					member.Setter = (bool)row.Cells[5].Value;

					members.Add(member);
				}
			}

			return members;
		}

	}
}
